//package com.plian.system.service.hr.abroad.impl;
//
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.itextpdf.text.*;
//import com.itextpdf.text.pdf.BaseFont;
//import com.itextpdf.text.pdf.PdfPCell;
//import com.itextpdf.text.pdf.PdfPTable;
//import com.itextpdf.text.pdf.PdfWriter;
//import com.plian.Tools.*;
//import com.plian.Tools.pagedata.PageData;
//import com.plian.system.constant.sys.FormStatusConstant;
//import com.plian.system.constant.sys.FormTypeConstant;
//import com.plian.system.dto.hr.abroad.AbroadPersonalDTO;
//import com.plian.system.entity.hr.abroad.AbroadPersonal;
//import com.plian.system.mapper.hr.abroad.AbroadPersonalMapper;
//import com.plian.system.mybatis.base.BaseFlowService;
//import com.plian.system.mybatis.base.BaseServiceImpl;
//import com.plian.system.service.hr.abroad.IAbroadPersonalService;
//import com.plian.system.service.sys.code.IFormCodeService;
//import com.plian.system.service.wf.FormWorkflowService;
//import com.plian.system.vo.hr.abroad.AbroadPersonalVO;
//import com.plian.system.vo.report.HeaderVO;
//import com.plian.system.vo.report.ReportReturnVO;
//import com.plian.system.vo.report.ReportVO;
//import com.plian.system.wrapper.hr.abroad.AbroadPersonalWrapper;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang.exception.ExceptionUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.stereotype.Service;
//
//import java.io.File;
//import java.io.FileOutputStream;
//import java.text.SimpleDateFormat;
//import java.util.List;
//import java.util.*;
//import java.util.stream.Collectors;
//
//import static com.plian.system.constant.sys.FormStatusConstant.UNREVIEWED;
//
///**
// * 因私出国（境）管理 服务实现类
// *
// * @author gangan
// * @since 2019-12-10
// */
//@Slf4j
//@Service
//@AllArgsConstructor
//public class AbroadPersonalServiceImpl extends BaseServiceImpl<AbroadPersonalMapper, AbroadPersonal> implements IAbroadPersonalService, BaseFlowService {
//
//    private FormWorkflowService formWorkflowService;
//
//	private IFormCodeService formCodeService;
//
//	private AbroadPersonalWrapper abroadPersonalWrapper;
//
//
//    @Override
//    public boolean deleteLogic(List<Long> ids) {
//    for (Long id : ids) {
//        AbroadPersonal abroadPersonal = getById(id);
//        if (abroadPersonal == null) {
//            return false;
//        } else if (abroadPersonal.getStatus() != null &&
//                (abroadPersonal.getStatus() != FormStatusConstant.STASH && abroadPersonal.getStatus() != FormStatusConstant.UNREVIEWED)) {
//            return false;
//        }
//        formWorkflowService.deleteProcessInstance(String.valueOf(id));
//    }
//    return super.deleteLogic(ids);
//    }
//
//    @Override
//    public HashMap<String,Object> commit(String id) {
//        handleStatus(Long.valueOf(id), FormStatusConstant.FLOW);
//
//		//将需要提交的流程变量返回，用于工作流侧
//		return new HashMap<String,Object>();
//    }
//
//    @Override
//    public Boolean finish(String id) {
//        return handleStatus(Long.valueOf(id), FormStatusConstant.FINISH);
//    }
//
//    @Override
//    public Boolean reject(String id, int formState, String comment) {
//        return handleStatus(Long.valueOf(id), formState);
//    }
//
//    /**
//     * 统一处理状态机事件方法
//     * @param id 主键
//     * @param updateStatus 更新状态
//     * @return boolean
//     */
//    private Boolean handleStatus(Long id, int updateStatus) {
//        try {
//            AbroadPersonal abroadPersonal = getById(id);
//            /**
//             * 设置审批人和审批时间
//             */
//            if (updateStatus == FormStatusConstant.FINISH){
//                abroadPersonal.setApprovedUser(TokenUtil.getTokenUserId());
//                Date now = DateUtil.now();
//                abroadPersonal.setApprovedTime(now);
//				if (StringUtil.isBlank(abroadPersonal.getFilingCode())){
//					AbroadPersonalVO abroadPersonalVO = abroadPersonalWrapper.entityToVO(abroadPersonal);
//					abroadPersonal.setFilingCode(abroadPersonalVO.getAbroadPersonalTypeName() +
//							formCodeService.getFilingCode(FormTypeConstant.HR_ABROAD_PERSONAL_FORM));
//				}
//            }
//            abroadPersonal.setStatus(updateStatus);
//            updateById(abroadPersonal);
//            return true;
//        } catch (Exception e) {
//            log.error(ExceptionUtils.getFullStackTrace(e));
//            return false;
//        }
//    }
//
//	@Override
//	public ReportReturnVO report(AbroadPersonalDTO abroadPersonalDTO){
//		ReportReturnVO reportReturnVO = new ReportReturnVO();
//		ReportVO reportVO = new ReportVO();
//
//		reportVO.addHeader(HeaderVO.builder().key("declaringUnitName").title("申报单位").build())
//			.addHeader(HeaderVO.builder().key("abroadPersonalTypeName").title("因私出国类型").build())
//			.addHeader(HeaderVO.builder().key("name").title("姓名").build())
//			.addHeader(HeaderVO.builder().key("companyName").title("所属单位").build())
//			.addHeader(HeaderVO.builder().key("JobName").title("职务").build())
//			.addHeader(HeaderVO.builder().key("visitsSubject").title("出访事由").build())
//			.addHeader(HeaderVO.builder().key("visitingCountriesAreas").title("出访国家和地区").build())
//			.addHeader(HeaderVO.builder().key("outsideTime").title("出国境时间").build())
//			.addHeader(HeaderVO.builder().key("stayDays").title("停留天数").build());
//
//		List<AbroadPersonalVO> list = abroadPersonalWrapper.entityToVO(
//		        baseMapper.reportDataPage(abroadPersonalDTO.getDeclaringUnitIds(),abroadPersonalDTO.getNameId(),
//		        		abroadPersonalDTO.getVisitsSubject(), abroadPersonalDTO.getVisitingCountriesAreas(),
//		        		abroadPersonalDTO.getOutsideTimeStart(), abroadPersonalDTO.getOutsideTimeEnd()));
//		int count = 0;
//		int sum = 0;
//		List<AbroadPersonalVO> list2 = new ArrayList<>() ;
//		if (CollectionUtil.isNotEmpty(list)){
//			//Comparator.comparing(RecruitmentManagementVO::getCompanyCode).reversed() 为降序
//			//按照申报单位编码升序，申报类型编码升序，组团单位编码升序，团长人员编码升序
//			list = list.parallelStream().sorted(Comparator.comparing(AbroadPersonalVO::getAbroadPersonalType).thenComparing(AbroadPersonalVO::getDeclaringUnitCode)
//					.thenComparing(AbroadPersonalVO::getOutsideTime)).collect(Collectors.toList());
////		list = list.parallelStream().sorted(Comparator.comparing(AbroadPersonalVO::getAbroadPersonalType).thenComparing(AbroadPersonalVO::getDeclaringUnitCode)
////			.thenComparing(AbroadPersonalVO::getNameCode).thenComparing(AbroadPersonalVO::getOutsideTime)).collect(Collectors.toList());
//
//			String companyNameSearchId = "";
//			for(int i =0;i< list.size();i++){
//				if(!companyNameSearchId.equals((list.get(i).getDeclaringUnitId()))){
//					if(i>0){
//						AbroadPersonalVO Total = new AbroadPersonalVO();
//						Total.setDeclaringUnitName("小计：" + count);
//						list2.add(Total);
//						sum+=count;
//					}
//					companyNameSearchId = list.get(i).getDeclaringUnitId();
//					count = 1;
//				}else{
//					count++;
//				}
//				list2.add(list.get(i));
//			}
//		}
//
//		sum+=count;
//		AbroadPersonalVO LastTotal = new AbroadPersonalVO();
//		LastTotal.setDeclaringUnitName("小计：" + count);
//		list2.add(LastTotal);
//
//		AbroadPersonalVO SumTotal = new AbroadPersonalVO();
//		SumTotal.setDeclaringUnitName("总计：" + sum);
//		list2.add(SumTotal);
//
//		reportVO.setData(list2);
//		reportReturnVO.setGrids(reportVO);
//		return reportReturnVO;
//	}
//
//	@Override
//	public File createReceptFile(Long id) {
//
//    	try{
//			int length = 0 ;
//			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//			AbroadPersonalVO bean = abroadPersonalWrapper.entityToVO(getById(id));
//
//			Document document = new Document(com.itextpdf.text.PageSize.A4, 90, 83, 30, 40);
//			FileOutputStream fs;
//			BaseFont bfHeiTi;
//			BaseFont bfST;
//			BaseFont test;
//			BaseFont bfbt;
//
//			File pdffile = File.createTempFile(id.toString(), ".pdf");
//			if (pdffile.exists()) {
//				pdffile.delete();
//			}
//			fs = new FileOutputStream(pdffile.getPath());//getAbsolutePath
//			PdfWriter.getInstance(document, fs);
//			// 打开文档
//			document.open();
//			bfST = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//			bfHeiTi = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//			bfbt = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//			test = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//			// 创建了一页
//			Font font_Zhengwen = new Font(bfST, 12.0f, Font.NORMAL, BaseColor.BLACK);
//			// 添加标题
//			Font font_Biaoti = new Font(bfHeiTi, 20.0f, Font.BOLD, BaseColor.BLACK);
//			Font font_biaotou1 = new Font(test, 12.0f, Font.NORMAL, BaseColor.BLACK);
//			Font font_biaotou2 = new Font(bfbt, 12.0f, Font.NORMAL, BaseColor.BLACK);
//
//			String number = bean.getDef1()==null?"":bean.getDef1();
//			Paragraph numberPa = new Paragraph("编号："+number, font_Zhengwen);
//			numberPa.setAlignment(2);
//			numberPa.setSpacingAfter(20f);
//			document.add(numberPa);
//
//			String titleWord = "市国资委因私出国(境)审签单";
//			Paragraph title = new Paragraph(titleWord,font_Biaoti);
//			title.setAlignment(1);
//			title.setSpacingAfter(0);
//			document.add(title);
//
//			float[] widths = { 40f, 50f, 90f, 60f, 50f, 70f };// 设置表格的列宽
//			PdfPTable table = new PdfPTable(widths);// 建立一个pdf表格
//			table.setSpacingBefore(10f);// 设置表格上面空白宽度
//			table.setSpacingAfter(0f);// 设置表格下面空白宽度
//			table.setTotalWidth(420);// 设置表格的宽度
//			PdfPCell cell = new PdfPCell();
//			PdfPCell cell2 = new PdfPCell();
//			cell.setMinimumHeight(25f);
//			cell2.setMinimumHeight(25f);
//			cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//			cell2.setVerticalAlignment(Element.ALIGN_MIDDLE);
//			table.setLockedWidth(true);
//
//			cell.setPhrase(new Paragraph("单位", font_biaotou2));
//			cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//			table.addCell(cell);
//			cell2.setPhrase(new Paragraph(bean.getDeclaringUnitName(),font_Zhengwen));
//			cell2.setHorizontalAlignment(Element.ALIGN_CENTER);
//			cell2.setColspan(5);
//			table.addCell(cell2);
//
//			cell.setPhrase(new Paragraph("因私\n出国\n（境）\n申请\n事项", font_biaotou2));
//			cell.setRowspan(2);
//			table.addCell(cell);
//
//			cell.setPhrase(new Paragraph("姓名", font_biaotou2));
//			cell.setRowspan(1);
//			table.addCell(cell);
//
//			cell.setPhrase(new Paragraph("单位及职务", font_biaotou2));
//			table.addCell(cell);
//
//			cell.setPhrase(new Paragraph("出访事由", font_biaotou2));
//			table.addCell(cell);
//
//			cell.setPhrase(new Paragraph("出访国家和地区", font_biaotou2));
//			table.addCell(cell);
//
//			cell.setPhrase(new Paragraph("出国(境)时间及\n" +
//				"停留天数\n", font_biaotou2));
//			table.addCell(cell);
//
//			cell2.setMinimumHeight(100);
//			cell2.setColspan(1);
//			cell2.setPhrase(new Paragraph(bean.getName(),font_Zhengwen));
//			table.addCell(cell2);
//
//			cell2.setPhrase(new Paragraph(bean.getCompanyName()+"\n"+bean.getJobNameName(),font_Zhengwen));
//			table.addCell(cell2);
//
//			cell2.setPhrase(new Paragraph(bean.getVisitsSubject(),font_Zhengwen));
//			table.addCell(cell2);
//
//			cell2.setPhrase(new Paragraph(bean.getVisitingCountriesAreas()));
//			table.addCell(cell2);
//
//			if(Optional.ofNullable(bean.getOutsideTime()).isPresent()){
//				cell2.setPhrase(new Paragraph(simpleDateFormat.format(bean.getOutsideTime())+",在外停留不超过"+bean.getStayDays()+"天",font_Zhengwen));
//			}else{
//				cell2.setPhrase(new Paragraph("在外停留不超过"+bean.getStayDays()+"天",font_Zhengwen));
//			}
//
//			table.addCell(cell2);
//
//			document.add(table);
//
//			float[] widths2 = { 40f, 140f, 40f, 140f };// 设置表格的列宽
//			PdfPTable table2 = new PdfPTable(widths2);// 建立一个pdf表格
//			table2.setSpacingBefore(0f);// 设置表格上面空白宽度
//			table2.setSpacingAfter(0f);// 设置表格下面空白宽度
//			table2.setTotalWidth(420);// 设置表格的宽度
//			table2.setLockedWidth(true);
//
//			cell.setMinimumHeight(150);
//			cell.setPhrase(new Paragraph("纪检组意见",font_biaotou2));
//			table2.addCell(cell);
//
//			cell2.setVerticalAlignment(Element.ALIGN_RIGHT);
//			cell2.setHorizontalAlignment(Element.ALIGN_RIGHT);
//			cell2.setPhrase(new Paragraph("\n\n\n\n\n\n\n\n审签人：" +
//				"\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0"+
//				"\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0"+
//				"\n\n\n年\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0月\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0日",font_Zhengwen));
//			table2.addCell(cell2);
//
//			cell.setPhrase(new Paragraph("组人处意见",font_biaotou2));
//			table2.addCell(cell);
//
//			cell2.setPhrase(new Paragraph("\n\n\n\n\n\n\n\n审签人：" +
//				"\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0"+
//				"\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0"+
//				"\n\n\n年\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0月\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0日",font_Zhengwen));
//			table2.addCell(cell2);
//
//			cell.setPhrase(new Paragraph("委领导意见",font_biaotou2));
//			table2.addCell(cell);
//
//			cell2.setColspan(3);
//			cell2.setPhrase(new Paragraph("\n\n\n\n\n\n\n\n审签人：" +
//				"\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0"+
//				"\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0"+
//				"\n\n\n年\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0月\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0日",font_Zhengwen));
//			table2.addCell(cell2);
//
//			document.add(table2);
//			document.close();
//			return pdffile;
//		}catch (Exception e){
//    		log.error(ExceptionUtils.getFullStackTrace(e));
//		}
//		return null;
//	}
//
//	@Override
//	public List<PageData> findByIds(List<String> formIds) {
//		LambdaQueryWrapper<AbroadPersonal> queryWrapper = new LambdaQueryWrapper<>();
//		queryWrapper.in(AbroadPersonal::getId, Func.toLongList(StringUtils.join(formIds, StringPool.COMMA)));
//		List<AbroadPersonal> abroadPersonals = list(queryWrapper);
//		List<AbroadPersonalVO> abroadPersonalsVOS = abroadPersonalWrapper.entityToVO(abroadPersonals);
//		List<PageData> pageDataList = new ArrayList<>();
//		try{
//			if (CollectionUtil.isNotEmpty(abroadPersonalsVOS)){
//				for (AbroadPersonalVO vo : abroadPersonalsVOS){
//					PageData pd = new PageData();
//					pd.put("id", vo.getId());
//					pd.put("UNITNAME", vo.getDeclaringUnitName());
//					pd.put("APPLICANTNAME", vo.getCreateUserName());
//					pd.put("applicantId", vo.getCreateUser());
//					pd.put("applyOrganizationId", vo.getDeclaringUnitId());
//					pd.put("formType", FormTypeConstant.HR_ABROAD_PERSONAL_FORM);
//					pageDataList.add(pd);
//				}
//			}
//
//			return pageDataList;
//		} catch (Exception e) {
//			log.error(ExceptionUtils.getFullStackTrace(e));
//		}
//		return pageDataList;
//	}
//
//	@Override
//	public List<Integer> getFormType() {
//		return Arrays.asList(FormTypeConstant.HR_ABROAD_PERSONAL_FORM);
//	}
//
//	@Override
//	public void goBackToStash(String formId) {
//		handleStatus(Long.valueOf(formId), UNREVIEWED);
//	}
//}
